<!--src/views/cases/EditCase.vue-->
<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElLoading } from 'element-plus'
import caseAPI from '@/api/case'

const route = useRoute()
const router = useRouter()
const caseId = route.params.id

const caseTypeOptions = [
  { value: '1', label: '民事案件' },
  { value: '2', label: '刑事案件' },
  { value: '3', label: '行政案件' },
  { value: '4', label: '其他案件' }
]

const statusOptions = [
  { value: '1', label: '待受理' },
  { value: '2', label: '处理中' },
  { value: '3', label: '已完成' },
  { value: '4', label: '已取消' },
  { value: '5', label: '审核中' },
  { value: '6', label: '已通过' },
  { value: '7', label: '已拒绝' }
]

const form = ref({
  caseName: '',
  caseType: '',
  status: '',
  description: '',
  lawyerName: ''
})

const loading = ref(false)

// 获取案件详情
const fetchCaseDetail = async () => {
  loading.value = true
  try {
    const res = await caseAPI.getCaseDetail(caseId)
    if (res.code === 200 && res.data) {
      form.value = {
        caseName: res.data.caseName || '',
        caseType: res.data.caseType || '',
        status: res.data.status || '',
        description: res.data.description || '',
        lawyerName: res.data.lawyerName || ''
      }
    } else {
      throw new Error(res.message || '获取案件详情失败')
    }
  } catch (error) {
    console.error('获取案件详情失败:', error)
    ElMessage.error(error.message || '获取案件详情失败')
    router.push('/cases')
  } finally {
    loading.value = false
  }
}

// 提交编辑
const submitForm = async () => {
  loading.value = true
  const loadingInstance = ElLoading.service({
    lock: true,
    text: '正在保存...',
    background: 'rgba(0, 0, 0, 0.7)'
  })

  try {
    const res = await caseAPI.updateCase(caseId, form.value)
    if (res.code === 200) {
      ElMessage.success('案件更新成功')
      if (route.query.from === 'list') {
        router.push('/cases')
      } else {
        router.push(`/cases/detail/${caseId}`)
      }
    } else {
      throw new Error(res.message || '更新案件失败')
    }
  } catch (error) {
    console.error('更新案件失败:', error)
    ElMessage.error(error.message || '更新案件失败')
  } finally {
    loading.value = false
    loadingInstance.close()
  }
}

// 取消编辑
const cancelEdit = () => {
  router.push(route.query.from === 'list' ? '/cases' : `/cases/detail/${caseId}`)
}

onMounted(() => {
  fetchCaseDetail()
})
</script>

<template>
  <div class="edit-case-container">
    <el-card shadow="never">
      <template #header>
        <div class="card-header">
          <h2>编辑案件</h2>
          <el-button type="text" @click="cancelEdit">返回</el-button>
        </div>
      </template>

      <el-form
          :model="form"
          label-width="120px"
          label-position="top"
          v-loading="loading"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="案件名称" prop="caseName">
              <el-input v-model="form.caseName" placeholder="请输入案件名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="案件类型" prop="caseType">
              <el-select
                  v-model="form.caseType"
                  placeholder="请选择案件类型"
                  style="width: 100%"
              >
                <el-option
                    v-for="item in caseTypeOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="案件状态" prop="status">
              <el-select
                  v-model="form.status"
                  placeholder="请选择案件状态"
                  style="width: 100%"
              >
                <el-option
                    v-for="item in statusOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责律师" prop="lawyerName">
              <el-input v-model="form.lawyerName" placeholder="请输入负责律师姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="案件描述" prop="description">
              <el-input
                  v-model="form.description"
                  type="textarea"
                  :rows="6"
                  placeholder="请输入案件详细描述"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24" class="form-actions">
            <el-button type="primary" @click="submitForm" :loading="loading">
              保存
            </el-button>
            <el-button @click="cancelEdit">取消</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.edit-case-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
</style>